﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      ACR RoleManagement UserControl Page
// Created By:     Tingrong Shi (trshi@wicresoft.com)
// Created Date:   2011-1-27
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASB.CC.Business.CIS;
using ASBCC.Business;
using ASBCC.Database.EntityFramework;

namespace BEGDC.CC.Web.ACR.Administration
{
    /// <summary>
    /// Role Management 公共控件
    /// </summary>
    public partial class RoleManegementCommon : EntityUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.EntityRole.Where = "it.BelongsTo=N'" + GetBelongsTo() + "' and !it.Disabled";
        }

        protected override EntityDataSource EntitySource
        {
            get { return this.EntityRole; }
        }

        protected override TextBox EntitySearchTxt
        {
            get { return this.SearchText; }
        }

        protected override Button EntityAddBtn
        {
            get { return this.BtnAdd; }
        }

        protected override Button EntitySearchBtn
        {
            get { return this.Searchbtn; }
        }

        protected override Button EntityClearBtn
        {
            get { return this.Cleanbtn; }
        }

        protected override GridView EntityGrid
        {
            get { return this.GVRoles; }
        }

        protected override DetailsView EntityDetails
        {
            get { return this.DVRole; }
        }

        private string GetBelongsTo()
        {
            HiddenField hr = this.Page.FindControl("BelongsTo") as HiddenField;
            if (hr == null)
            {
                throw new ArgumentException("Missing hidden for BelongsTo");
            }
            return hr.Value;
        }

        protected override void OnEntityDisabling(int entityKey)
        {
            RoleManagementHelper.SetRoleDisabled(entityKey);
            IPrincipal user = (IPrincipal)(Context.User);
            GlobalHelper.LogOperation(user.Identity.Name, SystemModule.ACR, "RoleManagement disabled: id=" + entityKey + "; name=" + RoleManagementHelper.getName(entityKey));
        }

        protected override string GetEntityWhereExpression(string keyword)
        {
            return "(it.Name like N'%" + keyword + "%')";
        }

        protected override void OnEntityInserting(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var role = e.Entity as ASBCC.Database.EntityFramework.Role;
            var pageList = this.Request.Form["pagecheck"];
            var menuList = this.Request.Form["menucheck"];
            RoleManagementHelper.OnRoleInserting(dc, role, pageList, menuList, GetBelongsTo());
        }

        protected override void OnEntityUpdating(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var role = e.Entity as ASBCC.Database.EntityFramework.Role;
            var pageList = this.Request.Form["pagecheck"];
            var menuList = this.Request.Form["menucheck"];
            RoleManagementHelper.OnRoleUpdating(dc, role, pageList, menuList, GetBelongsTo());
        }

        protected void EntityRole_Inserted(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var role = e.Entity as Role;
            IPrincipal user = (IPrincipal)(Context.User);
            var pages = LogInfoHelper.GetPageWithRole(role.RoleID);
            var menus = LogInfoHelper.GetMenuWithRole(role.RoleID);
            GlobalHelper.LogOperation(user.Identity.Name, SystemModule.ACR, "RoleManagement inserted: id=" + role.RoleID + ", name=" + role.Name + "; description=" + role.Description + ",isAdmin=" + role.IsAdmin + "; page=" + pages + "; menu=" + menus);
        }

        protected void EntityRole_Updated(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var role = e.Entity as Role;
            IPrincipal user = (IPrincipal)(Context.User);
            var pages = LogInfoHelper.GetPageWithRole(role.RoleID);
            var menus = LogInfoHelper.GetMenuWithRole(role.RoleID);
            GlobalHelper.LogOperation(user.Identity.Name, SystemModule.ACR, "RoleManagement updated: id=" + role.RoleID + ", name=" + role.Name + "; description=" + role.Description + ",isAdmin=" + role.IsAdmin + "; page=" + pages + "; menu=" + menus);
        }



    }
}